namespace PT.Common;

public class log
{
    
        /// <summary>
        /// 私有静态变量，用于lock参数
        /// </summary>
        private static Object MyObject = new object();


    /// <summary>
    /// 写日志
    /// </summary>
    /// <param name="str">字符串</param>
    /// <param name="FileName">日志文件名后缀(日期+后缀=文件名)</param>
    public static void WriteLog(string str, string FileName)
    {
        if (FileName == "") FileName = "日志";
        lock (MyObject)
        {
            //写日志
            if (true == true)
            {
                DateTime DT = System.DateTime.Now;
                string a = DT.Year.ToString() + "-" + DT.Month.ToString() + "-" + DT.Day.ToString();
                var logDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
                string FilePath = logDir + "/";

                //文件夹若不存在，则创建
                if (!Directory.Exists(FilePath))
                {
                    Directory.CreateDirectory(FilePath);
                }

                FileStream fs = new FileStream(FilePath + a + FileName + ".txt", FileMode.Append, FileAccess.Write, FileShare.Read);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(DT.ToString() + "\t" + str);
                sr.Close();
            }
        }
    }
    

}